package android.theHead;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;
import android.telephony.TelephonyManager;

public class HeadReadings implements Parcelable{
	
	public int key;
	public long utcTime = -1;
	public double longitude = -1;
	public double latitude = -1;
	public float tempo = 0;
	//TODO public float avg_tempo = -1;
	public float speed = 0;
	//TODO public float avg_speed = -1;
	public float bearing = 0;
	public double distance = 0;
	public float accuracy = -1;
	public double altitude = -999;
	public int sent = 0;
	public int teamID = -1;
	
	public HeadReadings(){
	}
	
	public HeadReadings(Location location){
		this.utcTime = location.getTime();
		this.longitude = location.getLongitude();
		this.latitude = location.getLatitude();
		this.speed = location.getSpeed();
		this.accuracy = location.getAccuracy();
		this.altitude = location.getAltitude();
		this.bearing = location.getBearing();
	}
	
	public HeadReadings(Parcel in) {
        readFromParcel(in);
    }

	public HeadReadings(Cursor c){
		this.bearing = c.getFloat(HeadDBAdapter.BEARING_COLUMN);
		this.distance = c.getDouble(HeadDBAdapter.DISTANCE_COLUMN);
		this.key = c.getInt(HeadDBAdapter.ID_COLUMN);
		this.latitude = c.getDouble(HeadDBAdapter.LAT_COLUMN);
		this.sent = c.getInt(HeadDBAdapter.SENT_COLUMN);
		this.longitude = c.getDouble(HeadDBAdapter.LONG_COLUMN);
		this.speed = c.getFloat(HeadDBAdapter.SPEED_COLUMN);
		this.tempo = c.getFloat(HeadDBAdapter.TEMPO_COLUMN);
		this.utcTime = c.getLong(HeadDBAdapter.TIME_COLUMN);
		this.accuracy = c.getFloat(HeadDBAdapter.ACCURACY_COLUMN);
		this.altitude = c.getDouble(HeadDBAdapter.ALTITUDE_COLUMN);
	}

	public HeadReadings(JSONObject jsonData){
		try {
			if (jsonData.has("longitude")){this.longitude = jsonData.getDouble("longitude");}
			if (jsonData.has("latitude")){this.latitude = jsonData.getDouble("latitude");}
			if (jsonData.has("tempo")){this.tempo = (float)jsonData.getDouble("tempo");}
			if (jsonData.has("speed")){this.speed = (float)jsonData.getDouble("speed");}
			if (jsonData.has("bearing")){this.bearing = (float)jsonData.getDouble("bearing");}
			if (jsonData.has("distance")){this.distance = jsonData.getDouble("distance");}
			if (jsonData.has("accuracy")){this.accuracy = (float)jsonData.getDouble("accuracy");}
			if (jsonData.has("altitude")){this.altitude = jsonData.getDouble("altitude");}
			if (jsonData.has("teamID")){this.teamID= jsonData.getInt("teamID");}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String stringRepr;
		stringRepr = "Location: " + this.latitude + ":" + this.longitude + "\n";
		stringRepr = "Distance: " + this.distance + "\n";
		stringRepr = "Bearing: " + this.bearing + "\n";
		stringRepr = "Tempo: " + this.tempo + "\n";
		stringRepr = "Time: " + this.utcTime + "\n";
		stringRepr = "Accurcay: " + this.accuracy + "\n";
		stringRepr = "Altitude: " + this.altitude + "\n";
		stringRepr = "Sent: " + this.sent + "\n";
		stringRepr = "Key: " + this.key;
		return stringRepr;
	}
	
	public Location getLocation(){
		Location loc = new Location("InternalDBEntry");
		loc.setBearing(this.bearing);
		loc.setLatitude(this.latitude);
		loc.setLongitude(this.longitude);
		loc.setSpeed(this.speed);
		loc.setTime(this.utcTime);
		loc.setAccuracy(this.accuracy);
		loc.setAltitude(this.altitude);
		return loc;
	}
	
	public JSONObject getJSON(){
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("utcTime", this.utcTime);
			jsonObject.put("longitude", this.longitude);
			jsonObject.put("latitude", this.latitude);
			jsonObject.put("tempo", this.tempo);
			jsonObject.put("speed", this.speed);
			jsonObject.put("bearing", this.bearing);
			jsonObject.put("distance", this.distance);
			jsonObject.put("accuracy", this.accuracy);
			jsonObject.put("altitude", this.altitude);
			jsonObject.put("key", this.key);
		} catch (JSONException e) {
    		e.printStackTrace();
    	}
		return jsonObject;
	}

	public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public HeadReadings createFromParcel(Parcel in) {
            return new HeadReadings(in);
        }
 
        public HeadReadings[] newArray(int size) {
            return new HeadReadings[size];
        }
    };
	
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(this.key);
		dest.writeLong(this.utcTime);
		dest.writeDouble(this.longitude);
		dest.writeDouble(this.latitude);
		dest.writeFloat(this.tempo);
		dest.writeFloat(this.speed);
		dest.writeFloat(this.bearing);
		dest.writeDouble(this.distance);
		dest.writeFloat(this.accuracy);
		dest.writeDouble(this.altitude);
		dest.writeInt(this.sent);
	}
	
	private void readFromParcel(Parcel in) {
		this.key = in.readInt();
		this.utcTime = in.readLong();
		this.longitude = in.readDouble();
		this.latitude = in.readDouble();
		this.tempo = in.readFloat();
		this.speed = in.readFloat();
		this.bearing = in.readFloat();
		this.distance = in.readDouble();
		this.accuracy = in.readFloat();
		this.altitude = in.readDouble();
		this.sent = in.readInt();
	}


}
